package com.anolesoft.yr.mbus.pre.crtl;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.pre.dto.*;
import com.anolesoft.yr.mbus.pre.entity.PreVisit;
import com.anolesoft.yr.mbus.pre.service.PreVisitService;
import com.anolesoft.yr.mbus.pre.vo.PreVisitQueryVO2;
import com.anolesoft.yr.mbus.util.GlobalLoginInfo;
import com.anolesoft.yr.mbus.util.StrCP;
import com.anolesoft.yr.util.PageInfo;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@CrossOrigin
@RequestMapping(value = "/preVisit")
public class PreVisitController {

    @Autowired
    PreVisitService preVisitService;

    //新增拜访客户记录
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult savePreVisit(@RequestBody PreVisit preVisit) throws Exception {
        String preVisitUid = StrCP.createObjuid32(UUID.randomUUID().toString());
        PreVisitSaveDTO preVisitSaveDTO = BeanUtil.copyProperties(preVisit, PreVisitSaveDTO.class);
        preVisitSaveDTO.setObjuid(preVisitUid);
        preVisitSaveDTO.setFacCode("");
        preVisitSaveDTO.setCcUserUid(GlobalLoginInfo.getGlobalLoginUser());
        preVisitSaveDTO.setCcTime(GlobalLoginInfo.getGlobalLoginTime());
        preVisitSaveDTO.setDelFlag(0);

        String str = "";
        try {
            str = preVisitService.savePreVisit(preVisitSaveDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }
        return new AjaxResult(200, str);
    }

    //修改拜访客户记录
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updatePreVisit(@RequestBody PreVisit preVisit) throws Exception {
        PreVisitUpdateDTO preVisitUpdateDTO = BeanUtil.copyProperties(preVisit, PreVisitUpdateDTO.class);
        preVisitUpdateDTO.setMmUserUid(GlobalLoginInfo.getGlobalLoginUser());
        preVisitUpdateDTO.setMmTime(GlobalLoginInfo.getGlobalLoginTime());

        String str = "";
        try {
            str = preVisitService.updatePreVisit(preVisitUpdateDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }
        return new AjaxResult(200, str);
    }

    //删除拜访记录
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult deletePreVisit(@RequestBody List<PreVisit> preVisitList) throws Exception {
        List preVisitDeleteDTOList = new ArrayList();
        PreVisitDeleteDTO preVisitDeleteDTO = null;

        for (int i = 0; i < preVisitList.size(); i++) {
            PreVisit preVisit = preVisitList.get(i);

            preVisitDeleteDTO = new PreVisitDeleteDTO();
            preVisitDeleteDTO.setObjuid(preVisit.getObjuid());
            preVisitDeleteDTOList.add(preVisitDeleteDTO);
        }

        String str = "";
        try {
            str = preVisitService.deletePreVisit(preVisitDeleteDTOList);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }
        return new AjaxResult(200, str);
    }

    //根据主键查询推广记录唯一数据
    @RequestMapping(value = "/queryByUid", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult AjaxResult(@RequestBody PreVisitQueryDTO preVisitQueryDTO) throws Exception {
        Map<String, Object> maps = preVisitService.queryPreVisitByUid(preVisitQueryDTO.getObjuid());
        PreVisitQueryVO2 preVisitQueryVO2 = (PreVisitQueryVO2) maps.get("preVisitQueryVO2");

        String jsons = "";
        jsons += "{";
        jsons += "\"visit\":";
        if (preVisitQueryVO2 != null) {
            jsons += "[";
            jsons += new Gson().toJson(preVisitQueryVO2);
            jsons += "]";
        }
        jsons += "}";

        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //查询拜访客户记录
    @RequestMapping(value = "/queryPreVisitAll", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryPreVisitAll(String visitTime, String clientName, String salesPerson, int page, int rows) throws Exception {
        Map<String, Object> maps = preVisitService.queryPreVisitAll(visitTime, clientName, salesPerson, page, rows);
        System.out.println("maps：" + maps);

        int count = (int) maps.get("count");
        List preVisitQueryVOList = (List) maps.get("preVisitQueryVOList");

        return new AjaxResult(200, "SUCCESS", new PageInfo(count, preVisitQueryVOList));
    }

}
